import time
import sys
import json
import io
import traceback
# 设置sys.stdout的编码为UTF-8
sys.stdout.reconfigure(encoding='utf-8')

from DrissionPage._pages.chromium_frame import ChromiumFrame
from DrissionPage._pages.chromium_tab import ChromiumTab


def frame_search_images(tab: ChromiumTab,searchImageBeans):
    for searchImage in searchImageBeans:
        try:
            openClickPath = searchImage['openClickPath']
            print(openClickPath)
            # 打开搜索窗口
            page_element = tab.ele('xpath:' + openClickPath)
            tab.scroll.to_see(page_element)
            time.sleep(1)
            page_element.click()
            time.sleep(2)

            searchTypeBean = searchImage['searchTypeBean']
            frame_search_image(tab, searchTypeBean)

        except:
            traceback.print_exc()


def frame_search_image(tab: ChromiumTab, searchTypeBean):
    # 查找iframe
    frameSrc = searchTypeBean['frameSrc']
    select_image_iframe = None
    page_iframes = tab.get_frames()
    for frame in page_iframes:
        frame_src = frame.attr('src')
        print(frame_src)
        if frame_src and frame_src.index(frameSrc) > 0:
            select_image_iframe = frame

    if select_image_iframe:
        time.sleep(2)
        container_search_image_input(select_image_iframe, searchTypeBean)

def container_search_image_input(frame: ChromiumFrame, searchTypeBean):

    searchText = searchTypeBean['searchText']
    clickButton = searchTypeBean['clickButton']
    sortByName = searchTypeBean['sortByName']

    searchInputPath = searchTypeBean['searchInputPath']
    searchClickPath = searchTypeBean['searchClickPath']
    resultSelectId = searchTypeBean['resultSelectId']
    selectOne = searchTypeBean['selectOne']

    # 根据名称排序
    if sortByName:
        sortClickPath = searchTypeBean['sortClickPath']
        sortUlPath = searchTypeBean['sortUlPath']
        sortText = searchTypeBean['sortText']
        sortClickEle = frame.ele('xpath:' + sortClickPath)
        sortClickEle.click()
        time.sleep(2)
        sort_list_ul = frame.ele('xpath:' + sortUlPath)
        sort_list_lis = sort_list_ul.children()
        if sort_list_lis:
            for sort_li in sort_list_lis:
                if sortText == sort_li.text:
                    sort_li.click()
                    time.sleep(1)

    # 搜索图片区域(弹出大框)
    # 文本输入框
    search_input = frame.ele('xpath:' + searchInputPath)
    #search_input.click()  # 点击获取焦点
    #time.sleep(2)
    search_input.clear()
    time.sleep(1)
    search_input.input(searchText)
    time.sleep(2)
    # 如果不相等, 则重新输入参数值
    if search_input.value != searchText:
        search_input.clear()
        time.sleep(1)
        search_input.input(searchText)
        time.sleep(2)

    # 搜索图片的案例和框
    search_span = frame.ele('xpath:' + searchClickPath)
    search_span.click()
    time.sleep(4)  # 第一次记载图片

    items_div = frame.ele("#" +resultSelectId)

    item_pic_images = items_div.eles('tag:img')

    if item_pic_images:
        # 循环所有图片,根据名称判定是否正确
        for item_img in item_pic_images:
            if item_img:
                item_img.parent().click()
                time.sleep(2)
                if selectOne:
                    break

    time.sleep(2)
    if clickButton:
        clickTextBegin = searchTypeBean['clickTextBegin']  # 确认开始
        submit_div = frame.ele('tag:span@@text()^' + clickTextBegin)  # 确认开始
        if submit_div:
            submit_div.click()
            time.sleep(2)




